package com.gsafety.wellsfort.service.security.impl;

import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.util.StringUtil;
import com.gsafety.wellsfort.dao.organization.ClassDAO;
import com.gsafety.wellsfort.dao.organization.SchoolDAO;
import com.gsafety.wellsfort.dao.organization.WxCustomerDAO;
import com.gsafety.wellsfort.dao.security.UserLoginDAO;
import com.gsafety.wellsfort.domain.bo.UserBO;
import com.gsafety.wellsfort.domain.model.organization.Class;
import com.gsafety.wellsfort.domain.model.organization.School;
import com.gsafety.wellsfort.domain.model.organization.Student;
import com.gsafety.wellsfort.domain.model.organization.WxCustomer;
import com.gsafety.wellsfort.domain.model.security.UserLogin;
import com.gsafety.wellsfort.domain.vo.UserVO;
import com.gsafety.wellsfort.service.security.IUserLoginService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class UserLoginServiceImpl implements IUserLoginService {
    @Autowired
    private UserLoginDAO userLoginDAO;

    @Autowired
    private SchoolDAO schoolDAO;

    @Autowired
    private ClassDAO classDAO;

    @Autowired
    private WxCustomerDAO wxCustomerDAO;

    @Override
    public UserLogin getUserLoginById(Integer id) {
        return userLoginDAO.selectByPrimaryKey(id);
    }

    @Override
    public UserLogin getUserLoginByAccount(String account) {
        return userLoginDAO.selectByAccount(account);
    }

    @Override
    public boolean add(UserLogin userLogin) {
        Date time = new Date();
        return userLoginDAO.insertSelective(userLogin) > 0 ? true : false;
    }

    @Override
    public boolean modifyPassword(String password) {
        UserLogin userLogin = UserLogin.builder().id(LoginContextUtil.getUid()).password(DigestUtils.md5Hex(password)).originalPassword(password).build();
        return userLoginDAO.modifyPassword(userLogin);
    }

    @Override
    public int update(UserLogin userLogin) {
        userLogin.setModified(new Date());
        return userLoginDAO.updateByPrimaryKeySelective(userLogin);
    }

    @Override
    public UserVO getDetail() {
        UserBO userBO = LoginContextUtil.getUserBo();
        Student student = LoginContextUtil.getStudent();
        if (userBO == null || student == null) {
            return null;
        }
        String schoolName = null, className = null;
        if (student != null) {
            School school = schoolDAO.selectByPrimaryKey(student.getSchoolId());
            schoolName = school.getName();
            Class cls = classDAO.selectByPrimaryKey(student.getClassId());
            className = cls.getName();
        }
        //若关联了微信，qq获取图像地址
        String headImgUrl = null;
        List<WxCustomer> wxCustomerList = wxCustomerDAO.selectByLoginId(userBO.getUid());
        if (wxCustomerList != null && wxCustomerList.size() > 0) {
            for (WxCustomer wxCustomer : wxCustomerList) {
                if (wxCustomer != null && StringUtils.isNotEmpty(wxCustomer.getHeadImgUrl())) {
                    headImgUrl = wxCustomer.getHeadImgUrl();
                    break;
                }
            }
        }
        String name = null;
        if ("0".equals(student.getExt1()) && StringUtil.isNotEmpty(student.getName())) {
            name = "游客" + student.getName().substring(student.getName().length() - 4);
        } else {
            name = student.getName();
        }
        return UserVO.builder().account(userBO.getAccount()).schoolName(schoolName)
                .className(className).name(name).
                        age(student.getAge() == null ? null : student.getAge().intValue()).parentName(student.getParentName()).
                        parentPhone(student.getParentPhone()).status(student.getStatus().intValue()).headUrl(headImgUrl).
                        build();
    }


    /**
     * 绑定设备信息
     *
     * @param userLogin
     * @return
     */
    @Override
    public int bindUserInfo(UserLogin userLogin) {
        return userLoginDAO.bindUserInfo(userLogin);
    }

    /**
     * app端注销 更改设备信id、设备来源
     *
     * @param id
     * @return
     */
    @Override
    public int unBindInfo(Integer id) {
        return userLoginDAO.unBindInfo(id);
    }


}
